<?php
namespace BoxApi\Controller;

use Think\Controller;

class IndexController extends CommonController {
	public function UserInfo() {
		echo 123;
	}

	/*******获得js,css资源******/
	public function getRes() {
		$allow_ext = array('js', 'css');
		$md5name = I('runThreadKey');
		$runThreadKey = md5(C('runThreadKey'));
		$url = parse_url(I('url'));
		$file = BOX_PATH . $url['path'];
		$info = explode(".", $url['path']);
		$ext = $info[count($info) - 1];
		if (!in_array($ext, $allow_ext)) {
			exit(json_encode(array('error' => 3, 'msg' => "No allow_ext")));
		}
		if (is_file($file)) {
			$content = file_get_contents($file);
		} else {
			exit(json_encode(array('error' => 2, 'msg' => "No file")));
		}
		if ($md5name == $runThreadKey) {
			header("Access-Control-Allow-Origin: *");
		}
		exit(json_encode(array('error' => 0, 'msg' => $content)));
	}

	/********网站初始化参数***/
	public function get_init_param() {

		if ($this->is_mobile) {
			Logic("BoxSns", "Member")->set_login();
			$this->user_id = session('user_id');
		}

		$appName = I('appName');
		$view_type = I('view_type', "web");
		$param = array(
			'appName' => I('appName'),
		);
		$data['params'] = array(
			'debug' => false, //是否调试
			'BoxVersion' => '2.8', //版本号
			'BoxVersionCache' => '2.4', //缓存号，重新发布
			'PageVersionCache' => 2, //页面缓存
			'view_type' => $view_type,
			'controllersPath' => PUBLIC_Controllers,
			'base_url' => HomeUrl,
			'res_url' => rtrim(PUBLIC_PATH, "/") . "/",
		);
		if ($this->user_id) {
			$data['user_info'] = BoxModel("system_member")->load($this->user_id);
			$data['uuid'] = $data['user_info']['uuid'];
		}
		$params = (array) dict($appName . 'Params');
		$data['params'] = array_merge($data['params'], $params);
		if ($data['params']['debug'] == "true") {
			$data['params']['BoxVersion'] = $data['params']['BoxVersion'] . time();
			$data['params']['PageVersionCache'] = $data['params']['PageVersionCache'] . time();
			$data['params']['BoxVersionCache'] = $data['params']['BoxVersionCache'] . time();
		}
		$params2 = dict($appName . 'Params' . $view_type);
		//移动
		if ($params2) {
			$data['params'] = array_merge($data['params'], (array) $params2);
		}

		hook("get_init_param");
		if ($param['appName']) {
			hook("get_init_param_" . $param['appName'], $data);
		}
		exit(json_encode($data));
	}

	public function logout() {
		Logic("BoxSns", "member")->logout();
		$this->jump("", "/");
	}

	public function auth_info() {
		$box_login_code = I('box_login_code');
		$data = Logic("BoxSns", "Member")->auth_info($box_login_code);
		echo json_encode($data);
	}

	public function noti() {
		// dump($this->user_id);
		if (!$this->user_id) {
			echo json_encode(array('error' => 1, 'msg' => '请先登录'));
		} else {
			$msg_count = BoxModel("sns_notification_count")->get(array('user_id' => $this->user_id));
			$type = array();
			foreach ($msg_count as $one) {
				$type[$one['type']] = $one['count'];
			}
			$list = BoxModel("sns_notification")->get(array('user_id' => $this->user_id, 'state' => 0), '5');
			echo json_encode(array('msg_count' => $type, 'list' => $list['list']));
			$this->incDuration();
		}
	}
//更新在线时长
	function incDuration() {
		if (!$this->user_id) {
			return;
		}
		$time = time();
		$duration = S("duration_" . $this->user_id);
		if (!$duration) {
			S("duration_" . $this->user_id, time());
			$duration = time();
		}
		$l = $time - $duration;
		if ($l > 9 && $l < 100) {
			BoxModel("system_member")->where(array('user_id' => $this->user_id))->setInc("duration", 10);
			S("duration_" . $this->user_id, $time);
			$r = BoxModel("edu_visit_time")->where(array('user_id' => $this->user_id, 'datetime' => date("Y-m-d")))->find();
			if ($r) {
				BoxModel("edu_visit_time")->where(array('user_id' => $this->user_id, 'datetime' => date("Y-m-d")))->setInc("duration", 10);
			} else {
				BoxModel("edu_visit_time")->add(array('user_id' => $this->user_id, 'datetime' => date("Y-m-d"), 'duration' => 10));
			}
		} else if ($l > 100) {
			S("duration_" . $this->user_id, $time);
		}

	}

	/********二维码**********/
	function QRcode() {
		$url = I('url');
		$QRcode = new \Common\Org\QRcode();
		ob_start();
		ob_clean();
		$QRcode->img(urldecode($url));
	}

	//上传图片
	function wxUpload() {
		$params = array('type' => 'return');
		$r = hook("Upload/Index/upload", $params);
		//print_r($r);
		exit(json_encode($r));
		//print_r($r);
	}

	//更新用户信息
	function updateInfo() {
		if (!intval($this->user_id)) {
			exit(json_encode(array('error' => 1, 'msg' => '请先登录')));
		}
		$keys = array(
			'address', 'nickname', 'sex', 'headimg', 'name',
		);
		$post = I();
		$param = array();
		foreach ($post as $k => $v) {
			if (in_array($k, $keys)) {
				$param[$k] = $v;
			}
		}
		if (!empty($param)) {
			$r = BoxModel("system_member")->where(array('user_id' => $this->user_id))->save($param);
			if ($r !== false) {
				exit(json_encode(array('error' => 0, 'msg' => '保存成功', 'data' => $param)));
			} else {
				exit(json_encode(array('error' => 3, 'msg' => '系统错误')));
			}
		} else {
			exit(json_encode(array('error' => 2, 'msg' => '参数不全')));
		}
	}
	// function test() {
	// 	$param = array(
	// 		'user_id' => '101081',
	// 		'title' => '小明在【小组】回复了你',
	// 		'topic' => '一起学习',
	// 		'info' => '回复内容',
	// 		'url' => '#',
	// 	);
	// 	Logs("wx_msg", $param);exit();
	// 	thread("BoxApi/Index/wx_msg", $param);
	// }
	//wx模板提醒
	function wx_msg() {
		$param = I();
		$md5name = md5(md5(C('runThreadKey')));
		$user_id = I('user_id');
		$user_ids = explode(",", $user_id);
		if ($md5name == $param['md5name']) {
			if ($user_ids) {
				foreach ($user_ids as $key => $user_id) {
					$r = wx_msg($param['title'], $user_id, $param['topic'], $param['info'], $param['url']);
					Logs("wx_msg", array_merge($param, (array) $r));
				}
			}

		} else {
			$param['error_msg'] = "密错错误";
			Logs("wx_msg_error", $param);
		}

		//dump($r);
	}

}